#include "qwscentral.h"

#include <iostream>

QWSCentral::QWSCentral(QWidget *parent)
    : QWidget(parent)
{
	containerLayout = new QHBoxLayout(this);

    createTableList();
    parsingRadioStations();

    setLayout(containerLayout);
}

void QWSCentral::createTableList() {
    tableView = new QTableView();

    tableModel = new QStandardItemModel();
    tableModel->setHorizontalHeaderLabels(QStringList() << tr("Nome") << tr("URL") << tr("Label") << tr("Bitrate") << tr("Freq"));
    tableView->setModel(tableModel);
    tableView->setAlternatingRowColors(true);
    tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    tableView->resizeColumnToContents(0);
    tableView->sortByColumn(0, Qt::AscendingOrder);
    tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
	tableView->setShowGrid(false);

    // signal and slot for the table
    connect(tableView, SIGNAL(clicked(const QModelIndex)), this, SLOT(radioSelectedEmitIndex(const QModelIndex)));

	containerLayout->addWidget(tableView);
}

QStringList QWSCentral::getResultStringList(const QString& queryString, const QUrl& documentUrl) {
    parser.setQuery(queryString, documentUrl);

    if ( !parser.isValid() ) {
        qDebug("Query: %s", queryString.toLatin1().data() );
        qFatal("Sorry, but the supplied query is invalid: \n-----\n%s-----\n", qPrintable( queryString ));
    }

    // ensure that the QStringList object is cleared before to be used by the parser.
    static QStringList resultStringList;
    resultStringList.clear();

    // evaluate the query and returns the result
    // \todo improve the standard output messages because the "parser.messageHandler()" is wrong!
    if ( !parser.evaluateTo(&resultStringList) ) {
        qFatal("A runtime error occurred while executing the query: \n-----\n%s-----\n", qPrintable( queryString ));
    }

    return resultStringList;
}

void QWSCentral::parsingRadioStations() {
    url.setUrl("/home/rex/aTunes/settings/preset_radio.xml");

    QStringList radio = getResultStringList(
        QLatin1String("for $section in doc(\"preset_radios.xml\")//list/Radio\n"
            "return string($section/name)\n"),
        url);

    QStringList radio_url = getResultStringList(
        QLatin1String("for $section in doc(\"preset_radios.xml\")//list/Radio\n"
        "return string($section/url)\n"),
        url);
    QStringList radio_label = getResultStringList(
        QLatin1String("for $section in doc(\"preset_radios.xml\")//list/Radio\n"
        "return string($section/label)\n"),
        url);
    QStringList radio_bitrate = getResultStringList(
        QLatin1String("for $section in doc(\"preset_radios.xml\")//list/Radio\n"
        "return string($section/bitrate)\n"),
        url);
    QStringList radio_freq = getResultStringList(
        QLatin1String("for $section in doc(\"preset_radios.xml\")//list/Radio\n"
        "return string($section/frequency)\n"),
        url);

    static int size = radio.size();
    static int i;

    for (i = 0; i < size; ++i) {
        RadioInfo radio_item(radio.at(i), radio_url.at(i), radio_label.at(i), radio_bitrate.at(i), radio_freq.at(i));
        radioList.insert(i, radio_item);
    }

    addStationToTable();
}

void QWSCentral::addStationToTable() {
    static int size = radioList.size();
    static int i;

    for (i = 0; i < size; ++i) {
        QStandardItem *item = new QStandardItem(radioList.at(i).getName());
        tableModel->setItem(i, 0, item);
        item = new QStandardItem(radioList.at(i).getUrl());
        tableModel->setItem(i, 1, item);
        item = new QStandardItem(radioList.at(i).getLabel());
        tableModel->setItem(i, 2, item);
        item = new QStandardItem(radioList.at(i).getBitrate());
        tableModel->setItem(i, 3, item);
        item = new QStandardItem(radioList.at(i).getFreq());
        tableModel->setItem(i, 4, item);
    }
}

void QWSCentral::resizeEvent(QResizeEvent *event){
	tableView->setColumnWidth(2, width() - tableView->autoScrollMargin() - 4);
	update();
	QWidget::resizeEvent(event);
}


void QWSCentral::radioSelectedEmitIndex(const QModelIndex index) {
	QModelIndexList indexList = tableView->selectionModel()->selectedIndexes();
	RadioInfo info = radioList.at(indexList.at(0).row());

	emit radioSel(info);
}

QWSCentral::~QWSCentral()
{

}
